<template>
  <el-row class="home-container" :gutter="20">
    <el-col :span="8" style="margin-top: 20px;">
      <el-card shadow="hover">
        <div class="user">
          <img :src="getImageUrl('user')" class="user" alt=""/>
          <div class="user-info">
            <p>admin</p>
            <p>超级管理员</p>
          </div>
        </div>
        <div class="login-info">
          <p>上次登录时间：<span>2025-06-10</span></p>
          <p>上次登录地点：<span>上海</span></p>
        </div>
      </el-card>

      <el-card shadow="hover" class="user-table">
        <el-table :data="tableData" style="width: 100%">
          <el-table-column
            v-for="(label, key) in tableLabel" :prop="key"
            :key="key" :label="label"/>
        </el-table>
      </el-card>
    </el-col>

    <el-col :span="16" style="margin-top: 20px;">
      <div class="num">
        <el-card shadow="hover" :body-style="{display: 'flex', padding: 0}"
                 v-for="item in countData" :key="item.name">
          <component :is="item.icon" class="icons" :style="{background: item.color}"></component>
          <div class="detail">
            <p class="num">￥{{ item.value }}</p>
            <p class="text">{{ item.name }}</p>
          </div>
        </el-card>
      </div>
      <el-card shadow="hover" class="top-echarts">
        <div ref="topEcharts" style="height: 280px;"></div>
      </el-card>

      <div class="graph">
        <el-card shadow="hover" class="top-echarts">
          <div ref="userEcharts" style="height: 240px;"></div>
        </el-card>
        <el-card shadow="hover" class="top-echarts">
          <div ref="videoEcharts" style="height: 240px;"></div>
        </el-card>
      </div>
    </el-col>
  </el-row>
</template>

<script setup lang="ts">
import { ref, reactive, onBeforeUnmount} from 'vue'
import * as echarts from 'echarts'
import {useImageUrl} from '@/assets/js/utils'
import {getTableData, getCountData, getChartData} from '@/api/mock/api'

const {getImageUrl} = useImageUrl()

const tableData = ref<any[]>([])

const tableLabel = ref({
  name: '课程',
  todayBuy: '今日购买',
  monthBuy: '本月购买',
  totalBuy: '总购买',
})

getTableData().then(res => {
  tableData.value = res.data.tableData
})

const countData = ref({})

getCountData().then(res => {
  countData.value = res.data
})

const observer = ref<ResizeObserver>()

//这个是折线图和柱状图 两个图表共用的公共配置
const xOptions = reactive({
  // 图例文字颜色
  textStyle: {
    color: '#333',
  },
  legend: {},
  grid: {
    left: '20%',
  },
  // 提示框
  tooltip: {
    trigger: 'axis',
  },
  xAxis: {
    type: 'category', // 类目轴
    data: [],
    axisLine: {
      lineStyle: {
        color: '#17b3a3',
      },
    },
    axisLabel: {
      interval: 0,
      color: '#333',
    },
  },
  yAxis: [
    {
      type: 'value',
      axisLine: {
        lineStyle: {
          color: '#17b3a3',
        },
      },
    },
  ],
  color: ['#2ec7c9', '#b6a2de', '#5ab1ef', '#ffb980', '#d87a80', '#8d98b3'],
  series: [],
})

const pieOptions = reactive({
  tooltip: {
    trigger: 'item',
  },
  legend: {},
  color: [
    '#0f78f4',
    '#dd536b',
    '#9462e5',
    '#a6a6a6',
    '#e1bb22',
    '#39c362',
    '#3ed1cf',
  ],
  series: []
})

const topEcharts = ref<HTMLDivElement>()
const userEcharts = ref<HTMLDivElement>()
const videoEcharts = ref<HTMLDivElement>()

getChartData().then(res => {
  const {orderData, userData, videoData} = res.data
  xOptions.xAxis.data = orderData.date
  xOptions.series = Object.keys(orderData.data[0]).map(value => {
    return {
      name: value,
      data: orderData.data.map(item => item[value]),
      type: 'line'
    }
  })
  const oneEchart = echarts.init(topEcharts.value)
  oneEchart.setOption(xOptions)

  xOptions.xAxis.data = userData.map(item => item.date)
  xOptions.series = [
    {
      name: '新增用户',
      data: userData.map(item => item.new),
      type: 'bar'
    },
    {
      name: '活跃用户',
      data: userData.map(item => item.active),
      type: 'bar'
    }
  ]
  const userEchart = echarts.init(userEcharts.value)
  userEchart.setOption(xOptions)

  pieOptions.series = [
    {
      data: videoData,
      type: 'pie'
    }
  ]
  const pieEchart = echarts.init(videoEcharts.value)
  pieEchart.setOption(pieOptions)

  observer.value = new ResizeObserver(en => {
    oneEchart.resize()
    pieEchart.resize()
    userEchart.resize()
  })

  if(topEcharts.value) {
    observer.value?.observe(topEcharts.value as Element)
  }
})

onBeforeUnmount(() => {
  observer.value?.disconnect()
})

</script>

<style lang="less" scoped>
.home-container{
  height: 100%;
  overflow: auto;
  .user{
    display: flex;
    align-items: center;
    border-bottom: 1px solid #ccc;
    margin-bottom: 20px;
    img{
      width: 150px;
      height: 150px;
      border-radius: 50%;
      margin-right: 40px;
    }
    .user-info{
      p{
        line-height: 40px;
        font-weight: 700;
        font-size: 35px;
        &:last-child{
          color: #999;
          font-size: 30px;
        }
      }
    }
  }
  .login-info{
    p{
      line-height: 30px;
      font-size: 14px;
      color: #666;
      span{
        color: #666;
        margin-left: 60px;
      }
    }
  }

  .user-table{
    margin-top: 20px;
  }
  .num{
    display: flex;
    flex-wrap: wrap;
    justify-content: space-between;

    .el-card{
      width: 32%;
      margin-bottom: 20px;
      .icons{
        width: 80px;
        height: 80px;
        font-size: 30px;
        text-align: center;
        color: white;
        line-height: 80px;
        flex-shrink: 0;
      }
      .detail{
        margin-left: 15px;
        display: flex;
        flex-direction: column;
        justify-content: center;
        .num{
          font-size: 30px;
          margin-bottom: 10px;
        }
        .text{
          font-size: 15px;
          text-align: center;
          color: #999;
          word-break: keep-all;
        }
      }
    }
  }
  .graph{
    margin-top: 20px;
    display: flex;
    justify-content: space-between;
    .el-card{
      width: 48%;
      height: 260px;
    }
  }
}
</style>